import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-05-01
 * Time: 22:04
 */
public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        Stack<Integer> stack = new Stack<>();
        int pop = 0; // 记录要弹出的下标
        for (int x : pushA) {
            stack.push(x);
            // 只要栈顶元素与当前要弹出的数字相同就弹出
            while (!stack.isEmpty() && stack.peek() == popA[pop]) {
                stack.pop();
                pop++;
            }
        }
        // 数字已经加入完了, 如果要弹出的数字还没有完, 说明就不能按照该顺序出栈
        return stack.isEmpty();
    }
}
